#ifndef _EVENT_H_
#define _EVENT_H_

#include <string>
#include "qbuffer.h"
#include "socket_base.h"

class SocketReactor;

class Event
{
public:
    Event(SocketReactor* reactor)
        : _reactor(reactor)
    {}
    virtual ~Event() {}

    virtual void Handle() = 0;
    int Id() const {return _sock->Id();}
protected:
    SocketReactor* _reactor {nullptr};

    Socket* _sock {s_invalid_socket};
};

class Acceptor 
    : public Event
{
public:
    Acceptor (SocketReactor* _reactor,
              unsigned short p,
              const std::string& label);
    ~Acceptor () {}

    virtual void Handle();

private:
    void init(unsigned short p);

    std::string _label;
};

class Reader 
    : public Event
{
public:
    Reader(SocketReactor* reactor, Socket* sock)
        : Event(reactor)
    {
        _sock = sock;
    }
    Reader(SocketReactor* reactor,
           const std::string& ip,
           unsigned short p);

    void SetLabel(const std::string& label) {_label = label;}
    virtual void Handle();
private:
    void renew();
private:
    ///label用于在接收数据时，标识存放数据的队列名，
    ///便于外部的对象通过队列使用该数据
    std::string _label;

    struct Buffer
    {
        SharedBuffer _cbuff{nullptr};
        long long   _length {0};
        long long   _recv {0};
    };
    
    Buffer* _buffer{nullptr};
};

#endif
